package extensionTools.smvCreator;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;


public class Test 
{
        private DiagramDatabase db;
        private FileWriter fstreamError;
        private FileWriter fstream;

        private BufferedWriter out;
        private BufferedWriter outError;   
        
        public Test() throws IOException
        {
                fstreamError = new FileWriter/*("/home/sdant/Desktop/Error.txt");*/("c:\\Temp\\temp.txt");
                outError = new BufferedWriter(fstreamError); 
                
                fstream = new FileWriter/*("/home/sdant/Desktop/Test1.txt");*/("c:\\Temp\\error.txt");//
                out = new BufferedWriter(fstream);
        }
        
        public Test(DiagramDatabase db) throws IOException
        {
            fstreamError = new FileWriter/*("/home/sdant/Desktop/Error.txt");*/("c:\\Temp\\temp.txt");//
            outError = new BufferedWriter(fstreamError); 
            
            fstream = new FileWriter/*("/home/sdant/Desktop/Test1.txt");*/("c:\\Temp\\temp.txt");//
            out = new BufferedWriter(fstream);
        
                this.db = db;
        }
        
        protected void finalize() throws Throwable 
        {
                out.close();
                outError.close();
        }
        
        public void printAllStates() throws IOException 
        {
            int indexForState =0;

            while(indexForState<db.getSmvStateList().size())
            {
                    State mySmvState = db.getSmvStateList().get(indexForState);
                    out.write("\nSTATE LIST:\nNEW STATE:\n");
                    out.write("State Id: " + mySmvState.getId() + "\n");
                    out.write("State Name: "+ mySmvState.getName()+ "\n");
                    out.write("State Object Id: " + mySmvState.getObjectId()+ "\n");   
                    
                    Enumeration<?> keys = mySmvState.getDestStateId().keys();
                    while (keys != null && keys.hasMoreElements())
                    {
                    	String label = (String)keys.nextElement();
                    	out.write("NEW DEST STATE:\nLabel: " + label);
                    	out.write("\nState DestStateId: " + mySmvState.getDestStateId().get(label) + "\n");                          	
                    }
                  
                    indexForState++;
            }
                        
        }
        public void printAllInstanceObjects() throws IOException
        {               
            int index =0;
                
                
                while(index<db.getSmvInstanceObjectList().size())
                {
                    SmvInstanceObject mySmvObject = db.getSmvInstanceObjectList().get(index);
                    out.write("\nInstance Objects LIST:\nNEW OBJECT:\n");
                    out.write("Object Id: " + mySmvObject.getId() + "\n");
                    out.write("Object Name: "+ mySmvObject.getName()+ "\n");
                    out.write("Object Enviormental: " + mySmvObject.isEnvironmental()+ "\n");
                    out.write("Object Value: " + mySmvObject.getValue()+ "\n");
                    out.write("Object Father Instance Name: " + mySmvObject.getFatherInstanceName()+ "\n");
                    
                    int indexForState =0;
                    while(indexForState<mySmvObject.getStateList().size())
                    {
                            out.write("\tNEW STATE:\n");
                            out.write("\tState Id: " + mySmvObject.getStateList().get(indexForState).getId()+ "\n");
                            out.write("\tState Name: " + mySmvObject.getStateList().get(indexForState).getName()+ "\n");
                            out.write("\tState Type: " + mySmvObject.getStateList().get(indexForState).getType()+ "\n");
                            if (mySmvObject.getStateList().get(indexForState).getObjectId()!=mySmvObject.getId())
                            {
                                    out.write("\tERROR: State Object Id is not good!!!!"+ "\n");
                            }
                            indexForState++;
                    }
                    
                    int indexForLink =0;
                    while(indexForLink<mySmvObject.getLinkList().size())
                    {
                            out.write("\tNEW LINK:\n ");
                            out.write("\tLink DstId: " + mySmvObject.getLinkList().get(indexForLink).getDstId()+ "\n");
                            out.write("\tLink DstType: " + mySmvObject.getLinkList().get(indexForLink).getDstType()+ "\n");
                            
                            out.write("\tLink SrcId: " + mySmvObject.getLinkList().get(indexForLink).getSrcId()+ "\n");
                            out.write("\tLink SrcType: " + mySmvObject.getLinkList().get(indexForLink).getSrcType()+ "\n");
                            
                            out.write("\tLink Type: " + mySmvObject.getLinkList().get(indexForLink).getLinkType()+ "\n");
                            
                            indexForLink++;
                    }
                    index++;
                        }
        }
        public void printAllObjects() throws IOException
        {               
            int index =0;
                
                
                while(index<db.getSmvObjectList().size())
                {
                    SmvObject mySmvObject = db.getSmvObjectList().get(index);
                    out.write("\nOBJECT LIST:\nNEW OBJECT:\n");
                    out.write("Object Id: " + mySmvObject.getId() + "\n");
                    out.write("Object Name: "+ mySmvObject.getName()+ "\n");
                    out.write("Object Enviormental: " + mySmvObject.isEnvironmental()+ "\n");
//                  
//                  int indexForState =0;
//                  while(indexForState<mySmvObject.getStateList().size())
//                  {
//                          out.write("\tNEW STATE:\n");
//                          out.write("\tState Id: " + mySmvObject.getStateList().get(indexForState).getId()+ "\n");
//                          out.write("\tState Name: " + mySmvObject.getStateList().get(indexForState).getName()+ "\n");
//                          out.write("\tState Type: " + mySmvObject.getStateList().get(indexForState).getType()+ "\n");
//                          if (mySmvObject.getStateList().get(indexForState).getObjectId()!=mySmvObject.getId())
//                          {
//                                  out.write("\tERROR: State Object Id is not good!!!!"+ "\n");
//                          }
//                          indexForState++;
//                  }
//                  
//                  int indexForLink =0;
//                  while(indexForLink<mySmvObject.getLinkList().size())
//                  {
//                          out.write("\tNEW LINK:\n ");
//                          out.write("\tLink DstId: " + mySmvObject.getLinkList().get(indexForLink).getDstId()+ "\n");
//                          out.write("\tLink DstType: " + mySmvObject.getLinkList().get(indexForLink).getDstType()+ "\n");
//                          
//                          out.write("\tLink SrcId: " + mySmvObject.getLinkList().get(indexForLink).getSrcId()+ "\n");
//                          out.write("\tLink SrcType: " + mySmvObject.getLinkList().get(indexForLink).getSrcType()+ "\n");
//                          
//                          out.write("\tLink Type: " + mySmvObject.getLinkList().get(indexForLink).getLinkType()+ "\n");
//                          
//                          indexForLink++;
//                  }
                    index++;
                        }
        }
        public void printAllProcess() throws IOException
        {               
            int index =0;
                
                
                while(index<db.getSmvProcessList().size())
                {
                    SmvProcess mySmvProcess = db.getSmvProcessList().get(index);
                    out.write("\nPROCESS LIST:\nNEW PROCESS:\n");
                    out.write("Process Id: " + mySmvProcess.getId() + "\n");
                    out.write("Process Name: "+ mySmvProcess.getName()+ "\n");
                    out.write("Process Father Id: " + mySmvProcess.getFatherId()+ "\n");
                    out.write("Process TimeLine: " + mySmvProcess.getTimeLine() + "\n");
                    out.write("Is process father to leaf: " + mySmvProcess.getFatherToLeafs() + "\n");
                    
//                    int indexForOrXor = 0;
//                    while(indexForOrXor < mySmvProcess.getOrLists().size())
//                    {
//                        out.write("\t\tNEW OR LIST:\n ");
//                        
//                        ArrayList<Link> temp = mySmvProcess.getOrLists().get(indexForOrXor);
//                        int i=0;
//                        while(i<temp.size())
//                        {
//                            out.write("\t\tNEW LINK:\n ");
//                            out.write("\t\t\tLink DstId: " + temp.get(i).getDstId()+ "\n");
//                            out.write("\t\t\tLink DstType: " + temp.get(i).getDstType()+ "\n");
//                            
//                            out.write("\t\t\tLink SrcId: " + temp.get(i).getSrcId()+ "\n");
//                            out.write("\t\t\tLink SrcType: " + temp.get(i).getSrcType()+ "\n");
//                            
//                            out.write("\t\t\tLink Type: " + temp.get(i).getLinkType()+ "\n");
//                            i++;
//                        }  
//                        indexForOrXor++;
//                    }
                        
//                    indexForOrXor = 0;
//                    while(indexForOrXor < mySmvProcess.getXorLists().size())
//                    {
//                        out.write("\t\tNEW XOR LINK:\n ");
//                        
//                        ArrayList<Link> temp = mySmvProcess.getXorLists().get(indexForOrXor);
//                        int i=0;
//                        while(i<temp.size())
//                        {
//                             out.write("\t\tNEW LINK:\n ");
//                             out.write("\t\t\tLink DstId: " + temp.get(i).getDstId()+ "\n");
//                             out.write("\t\t\tLink DstType: " + temp.get(i).getDstType()+ "\n");
//                             
//                             out.write("\t\t\tLink SrcId: " + temp.get(i).getSrcId()+ "\n");
//                             out.write("\t\t\tLink SrcType: " + temp.get(i).getSrcType()+ "\n");
//                             
//                             out.write("\t\t\tLink Type: " + temp.get(i).getLinkType()+ "\n");
//                             i++;
//                        }
//                        
//                        indexForOrXor++;
//                    }
                                    
                    int indexForChild = 0;
                    while(indexForChild<mySmvProcess.getChildrenIdList().size())
                    {
                            out.write("\tNEW CHILD:\n");
                            out.write("\tChild Id: " + mySmvProcess.getChildrenIdList().get(indexForChild) + "\n");
                            indexForChild++;
                    }
                    
                    int indexForLink = 0;
                    while(indexForLink < mySmvProcess.getLinkList().size())
                    {
                            out.write("\tNEW LINK:\n ");
                            out.write("\tLink DstId: " + mySmvProcess.getLinkList().get(indexForLink).getDstId()+ "\n");
                            out.write("\tLink DstType: " + mySmvProcess.getLinkList().get(indexForLink).getDstType()+ "\n");
                            
                            out.write("\tLink SrcId: " + mySmvProcess.getLinkList().get(indexForLink).getSrcId()+ "\n");
                            out.write("\tLink SrcType: " + mySmvProcess.getLinkList().get(indexForLink).getSrcType()+ "\n");
                            
                            out.write("\tLink Type: " + mySmvProcess.getLinkList().get(indexForLink).getLinkType()+ "\n");  
                            indexForLink++;
                    }
                    index++;
                }
    }
        
        public void printAllUpdateProcess() throws IOException
        {               
            int index =0;
            
                while(index<db.getUpdateProcessList().size())
                {
                    SmvProcess mySmvProcess = db.getUpdateProcessList().get(index);
                    out.write("\nPROCESS LIST:\nNEW PROCESS:\n");
                    out.write("Process Id: " + mySmvProcess.getId() + "\n");
                    out.write("Process Name: "+ mySmvProcess.getName()+ "\n");
                    out.write("Process Father Id: " + mySmvProcess.getFatherId()+ "\n");
                    out.write("Process TimeLine: " + mySmvProcess.getTimeLine() + "\n");
                    out.write("Is process father to leaf: " + mySmvProcess.getFatherToLeafs() + "\n");                
                    
                    int indexForLink = 0;
                    while(indexForLink < mySmvProcess.getLinkList().size())
                    {
                            out.write("\tNEW LINK:\n ");
                            out.write("\tLink DstId: " + mySmvProcess.getLinkList().get(indexForLink).getDstId()+ "\n");
                            out.write("\tLink DstType: " + mySmvProcess.getLinkList().get(indexForLink).getDstType()+ "\n");
                            
                            out.write("\tLink SrcId: " + mySmvProcess.getLinkList().get(indexForLink).getSrcId()+ "\n");
                            out.write("\tLink SrcType: " + mySmvProcess.getLinkList().get(indexForLink).getSrcType()+ "\n");
                            
                            out.write("\tLink Type: " + mySmvProcess.getLinkList().get(indexForLink).getLinkType()+ "\n");  
                            indexForLink++;
                    }
                    index++;
                }
        }
        
        public void printErrorMsg(String string) throws IOException
        {    
                outError.write(string + "\n");
        }

        public void printRule(String rule) throws IOException {
                out.write(rule);        
        }

                public void printAllLinks() throws IOException {
                        int index = 0;
                        ArrayList<SmvObject> objs = db.getSmvObjectList();
                        int objsSize = objs.size();
                         while(index< objsSize)
             {
                 SmvObject mySmvObject = objs.get(index);
                 System.out.println("Object Id: " + mySmvObject.getName() + "\n");
                 System.out.println("This is the links he's connected to:\n");
                 ArrayList<Link> links = mySmvObject.getLinkList();
                 for (Link link : links) {
                                        if(link.getSrcId() == mySmvObject.getId() || mySmvObject.idBelongsToObj(link.getSrcId()) != null){
                                                System.out.println("he is src at "+link.getLinkType() + " "+RulesUtils.getLinkEdgeName(link, db, false)+"\n");
                                        }
                                        else{
                                                System.out.println("he is dst at "+link.getLinkType() + " "+RulesUtils.getLinkEdgeName(link, db, true)+"\n");
                                        }
                }
                 index++;
             }
                }

}
